package dryven.view.engine.component.childzone;

import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import dryven.view.ViewDataDictionary;

public class MethodCallback implements ChildZoneCallback {
	
	private Object _viewObject;
	private Method _callBack;
	
	public MethodCallback(Object viewObject, String methodName) {
		try {
			_callBack = _viewObject.getClass().getMethod(methodName, new Class<?>[]{ViewDataDictionary.class,Writer.class});
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	@Override
	public void render(ViewDataDictionary viewData, Writer out) {
		try {
			_callBack.invoke(_viewObject, new Object[]{viewData,out});
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
